package com.ysd.lis.service.eq.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.eq.EqManufacturer;
import com.ysd.lis.mapper.eq.EqManufacturerMapper;
import com.ysd.lis.request.EqManufacturerDto;
import com.ysd.lis.request.qc.ManufacturerPageParam;
import com.ysd.lis.service.eq.EqManufacturerService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备管理-生产商 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-07-30
 */
@Service
public class EqManufacturerServiceImpl extends ServiceImpl<EqManufacturerMapper, EqManufacturer> implements EqManufacturerService {
    @Autowired
    private EqManufacturerMapper eqManufacturerMapper;

    @Override
    public Result findManufacturerPageList(ManufacturerPageParam pageParam) {
        Page<EqManufacturer> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        LambdaQueryWrapper<EqManufacturer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EqManufacturer::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(EqManufacturer::getManuName, pageParam.getSeachValue())
                    .or().like(EqManufacturer::getManuCode, pageParam.getSeachValue())
                    .or().like(EqManufacturer::getInputCode, pageParam.getSeachValue()));
        }

        if (ToolsUtils.isNotEmpty(pageParam.getIsStop())) {
            lambdaQueryWrapper.eq(EqManufacturer::getIsStop, pageParam.getIsStop());
        }
        lambdaQueryWrapper.orderByAsc(EqManufacturer::getSeq);
        Page<EqManufacturer> manufacturerPage = eqManufacturerMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", manufacturerPage);
    }

    @Override
    public Result addManufacturer(EqManufacturer manufacturer) {
        int insert = eqManufacturerMapper.insert(manufacturer);
        if (insert > 0) {
            return Result.succ(1, "添加成功", manufacturer);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result editManufacturer(EqManufacturer manufacturer) {
        int updateById = eqManufacturerMapper.updateById(manufacturer);
        if (updateById > 0) {
            return Result.succ(1, "提交成功", manufacturer);
        } else {
            return Result.fail(400, "修改失败", "");
        }
    }

    @Override
    public Result deleteManufacturer(EqManufacturer manufacturer) {
        int deleteById = eqManufacturerMapper.deleteById(manufacturer.getId());
        if (deleteById > 0) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result batchDeleteManufacturerList(EqManufacturerDto dto) {
        List<EqManufacturer> manufacturerList = dto.getManufacturerList();
        List<String> ids = manufacturerList.stream().map(EqManufacturer::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

}
